package com.csair.platform.cit.service.biz.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.csair.invoice.common.base.util.CastUtil;
import com.csair.invoice.common.base.util.StringUtil;
import com.csair.invoice.common.base.vo.JsonResult;
import com.csair.platform.cit.facade.model.CitQueryParam;
import com.csair.platform.cit.facade.model.InitDataVo;
import com.csair.platform.cit.service.biz.InitDataService;
import com.csair.platform.usercenter.facade.model.user.UserVo;

import lombok.extern.slf4j.Slf4j;

/**
 * [期初数据]服务实现类
 * @author ：caoyu
 * 
 */
@Slf4j
@Service
public class InitDataServiceImpl extends InitBaseServiceImpl implements InitDataService {

	
	@SuppressWarnings("unused")
	private InitDataVo getTestData() {
		 InitDataVo result = new InitDataVo();
		 //===========从业人数====================
         result.setQccyrs1("200"); //一季度期初从业人数
         result.setQmcyrs1("200"); //一季度期末从业人数
         result.setQccyrs2("200"); //二季度期初从业人数
         result.setQmcyrs2("200"); //二季度期末从业人数
         result.setQccyrs3("200"); //三季度期初从业人数
         result.setQmcyrs3("200"); //三季度期末从业人数
         result.setQccyrs4("200"); //四季度期初从业人数
         result.setQmcyrs4("200"); //四季度期末从业人数
         //===========资产总额（万元）====================
         result.setQczcze1("1200"); //一季度期初资产总额
         result.setQmzcze1("1200"); //一季度期末资产总额
         result.setQczcze2("1200"); //二季度期初资产总额
         result.setQmzcze2("1200"); //二季度期末资产总额
         result.setQczcze3("1200"); //三季度期初资产总额
         result.setQmzcze3("1200"); //三季度期末资产总额
         result.setQczcze4("1200"); //四季度期初资产总额
         result.setQmzcze4("1200"); //四季度期末资产总额
       //===========主表信息====================
         result.setHc18_zjgftbl("1200"); //主表信息-行次18：总机构填报-总机构分摊比例
         result.setHc19_zjgczjzftbl("1200"); //主表信息-行次19：总机构填报-财政集中分配比例
         result.setHc20_fzjgftbl("1200"); //主表信息-行次20：总机构填报-全部分支机构分摊比例
         //===========主表信息====================
         result.setHc14_sjyyjsdseLj("1200"); //主表信息-行次14：实际已缴所得税额累计
         result.setHc15_tdywyjzsdseLj("1200"); //主表信息-行次15：特定业务预缴（征）所得税额累计
         return result;
	}
	
    @Override
    public JsonResult<InitDataVo> getInitData(CitQueryParam param, UserVo user) {
        InitDataVo result = new InitDataVo();
        /* 造假数据
        if(user != null) {
        	return JsonResult.success(this.getTestData());
        }
        */
        String returnStr = getInitJsonData(param, user);
        if (StringUtil.isEmpty(returnStr)) {
            return JsonResult.fail(500, "调用税局接口异常");
        }
        JSONObject head = JSON.parseObject(returnStr).getJSONObject("head");
        String headErrorCode = head.getString("errorCode");
        if (!"0".equals(headErrorCode)) {
            String headErrorMsg = head.getString("errorMsg");
            return JsonResult.fail(CastUtil.castInt(headErrorCode), headErrorMsg);
        }
        JSONObject dataResult = JSON.parseObject(returnStr);
        String bodyStr = dataResult.getString("body"); 
        JSONObject dataBody = JSON.parseObject(bodyStr); 
        String returnFlagStr = dataBody.getString("returnFlag"); 
        if (!"Y".equals(returnFlagStr)) {
            String errInfoStr = dataBody.getString("errInfo");
            JSONObject dataErrInfo = JSON.parseObject(errInfoStr); 
            String errorMsg = dataErrInfo.getString("msg");
            return JsonResult.fail(500, errorMsg);
        }
        String body2Str = dataBody.getString("body"); 
        JSONObject dataBody2 = JSON.parseObject(body2Str); 
        String hqStr = dataBody2.getString("hq_"); 
        JSONObject dataHq = JSON.parseObject(hqStr); 
        String sqsbxxStr = dataHq.getString("sqsbxx"); //上期申报信息
        JSONObject object = JSON.parseObject(sqsbxxStr); 
        if (object != null) {
            //===========从业人数====================
            result.setQccyrs1(object.getString("qccyrs1")); //一季度期初从业人数
            result.setQmcyrs1(object.getString("qmcyrs1")); //一季度期末从业人数
            result.setQccyrs2(object.getString("qccyrs2")); //二季度期初从业人数
            result.setQmcyrs2(object.getString("qmcyrs2")); //二季度期末从业人数
            result.setQccyrs3(object.getString("qccyrs3")); //三季度期初从业人数
            result.setQmcyrs3(object.getString("qmcyrs3")); //三季度期末从业人数
            result.setQccyrs4(object.getString("qccyrs4")); //四季度期初从业人数
            result.setQmcyrs4(object.getString("qmcyrs4")); //四季度期末从业人数
            //===========资产总额（万元）====================
            result.setQczcze1(object.getString("qczcze1")); //一季度期初资产总额
            result.setQmzcze1(object.getString("qmzcze1")); //一季度期末资产总额
            result.setQczcze2(object.getString("qczcze2")); //二季度期初资产总额
            result.setQmzcze2(object.getString("qmzcze2")); //二季度期末资产总额
            result.setQczcze3(object.getString("qczcze3")); //三季度期初资产总额
            result.setQmzcze3(object.getString("qmzcze3")); //三季度期末资产总额
            result.setQczcze4(object.getString("qczcze4")); //四季度期初资产总额
            result.setQmzcze4(object.getString("qmzcze4")); //四季度期末资产总额
        }
        String qtxxStr = dataHq.getString("qtxx"); //其他信息
        JSONObject objectQt = JSON.parseObject(qtxxStr); 
        if (objectQt != null) {
            //===========主表信息====================
            result.setHc18_zjgftbl(objectQt.getString("zjgFtbl")); //主表信息-行次18：总机构填报-总机构分摊比例
            result.setHc19_zjgczjzftbl(objectQt.getString("zjgCzjzFtbl")); //主表信息-行次19：总机构填报-财政集中分配比例
            result.setHc20_fzjgftbl(objectQt.getString("fzjgFtbl")); //主表信息-行次20：总机构填报-全部分支机构分摊比例
        }
        String htStr = dataBody2.getString("ht_"); 
        JSONObject dataHt = JSON.parseObject(htStr); 
        String ywbwStr = dataHt.getString("ywbw"); 
        JSONObject dataYwbw = JSON.parseObject(ywbwStr); 
        String a200000YwbdStr = dataYwbw.getString("A200000Ywbd"); 
        JSONObject dataA200000Ywbd = JSON.parseObject(a200000YwbdStr); 
        String sbxxStr = dataA200000Ywbd.getString("sbxx"); 
        JSONObject objectSbxx = JSON.parseObject(sbxxStr); 
        if (objectSbxx != null) {
            //===========主表信息====================
            result.setHc14_sjyyjsdseLj(objectSbxx.getString("sjyyjsdseLj")); //主表信息-行次14：实际已缴所得税额累计
            result.setHc15_tdywyjzsdseLj(objectSbxx.getString("tdywyjzsdseLj")); //主表信息-行次15：特定业务预缴（征）所得税额累计
        }
        log.info("返回前端的[期初数据]为：" + result.toString());
        return JsonResult.success(result);
    }
}
